package top.zlcode.system.controller.exam;

import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.zlcode.common.core.controller.BaseController;
import top.zlcode.common.core.domain.R;
import top.zlcode.common.core.domain.TableDataInfo;
import top.zlcode.system.domain.exam.dto.ExamAddDTO;
import top.zlcode.system.domain.exam.dto.ExamQueryDTO;
import top.zlcode.system.domain.exam.dto.ExamQuestionAddDTO;
import top.zlcode.system.domain.exam.vo.ExamDetailVO;
import top.zlcode.system.domain.exam.dto.ExamEditDTO;
import top.zlcode.system.service.exam.IExamService;

@RestController
@RequestMapping("/exam")
public class ExamController extends BaseController {

    @Autowired
    private IExamService examService;

    @GetMapping("/list")
    @Operation(summary = "查询竞赛列表", description = "根据 examQueryDTO 参数查询竞赛列表")
    public TableDataInfo list(ExamQueryDTO examQueryDTO) {
        return getTableDataInfo(examService.list(examQueryDTO));
    }

    @PostMapping("/add")
    @Operation(summary = "新增一个竞赛", description = "根据 examAddDTO 参数新增竞赛")
    public R<String> add(@Valid @RequestBody ExamAddDTO examAddDTO) {
        return R.ok(examService.examAdd(examAddDTO));
    }

    @PostMapping("/question/add")
    @Operation(summary = "往竞赛里面添加题目", description = "根据 examQuestionAddDTO 参数往竞赛中添加题目")
    public R<Void> questionAdd(@RequestBody ExamQuestionAddDTO examQuestionAddDTO) {
        return toR(examService.questionAdd(examQuestionAddDTO));
    }

    @DeleteMapping("/question/delete")
    @Operation(summary = "删除竞赛里面的题目", description = "根据 examId, questionId 删除竞赛中的题目")
    public R<Void> questionDelete(Long examId, Long questionId) {
        return toR(examService.questionDelete(examId, questionId));
    }

    @GetMapping("/detail")
    @Operation(summary = "获取竞赛里面的题目信息", description = "根据 examId 获取这个竞赛下的题目")
    public R<ExamDetailVO> detail(Long examId) {
        return R.ok(examService.detail(examId));
    }

    @PutMapping("/edit")
    @Operation(summary = "获取竞赛的基本信息", description = "根据 examEditDTO 修改竞赛的基本信息")
    public R<Void> edit(@Valid @RequestBody ExamEditDTO examEditDTO) {
        return toR(examService.edit(examEditDTO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除整个竞赛", description = "根据 examId 删除整个竞赛")
    public R<Void> delete(Long examId) {
        return toR(examService.delete(examId));
    }

    @PutMapping("/publish")
    @Operation(summary = "发布竞赛", description = "根据 examId 发布当前竞赛")
    public R<Void> publish(Long examId) {
        return toR(examService.publish(examId));
    }

    @PutMapping("/cancelPublish")
    @Operation(summary = "取消发布竞赛", description = "根据 examId 取消发布当前竞赛")
    public R<Void> cancelPublish(Long examId) {
        return toR(examService.cancelPublish(examId));
    }
}
